package com.lgqz.himalayafm.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;

import com.lgqz.himalayafm.DrawListener;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.activity.MainActivity;

import com.lgqz.himalayafm.adapter.RadioAdapter;
import com.lgqz.himalayafm.adapter.RadioFristListAdapter;
import com.lgqz.himalayafm.adapter.RadioSeconListAdapter;
import com.lgqz.himalayafm.bean.RadioEntity;
import com.lgqz.himalayafm.http.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 晨风北林
 * 广播的Fragment
 */
public class RadiosFragment extends Fragment implements Callback<RadioEntity>,View.OnClickListener {

    private View view;
    private RecyclerView mRecycleview;
    private List<String> list;
    private List<String> list1;
    private ImageView mImage;
    private RadioAdapter adapter;
    private ListView mradioSecondList;
    private ListView mradioFristList;
    private RadioButton mBtnHomeStation;
    private DrawListener listener;
    private RadioButton mCountryStation;
    private RadioButton mNetStation;
    private RadioButton mProvinceStation;

    public RadiosFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_radios, container, false);
        initData();//添加数据
        /**
         * 下载数据u
         */
        listener= (DrawListener) getActivity();
        downLoadData();
        initView();
        initListener();
        return view;
    }

    /**
     * 添加所有布局监听器
     */
    private void initListener() {

        mBtnHomeStation.setOnClickListener(this);
        mCountryStation.setOnClickListener(this);
        mNetStation.setOnClickListener(this);
        mProvinceStation.setOnClickListener(this);
    }

    /**
     * 布局控件的实例化
     */
    private void initView() {
        //获的第一个listview的实例
        mradioFristList = (ListView) view.findViewById(R.id.radio_fristlist_id);
        mradioSecondList = (ListView) view.findViewById(R.id.radio_secondlist_id);
        mRecycleview = (RecyclerView) view.findViewById(R.id.id_recyclerview);
        mBtnHomeStation = (RadioButton) view.findViewById(R.id.home_station);
        //获得国家台的实例
        mCountryStation = ((RadioButton) view.findViewById(R.id.country_station));
        //获得网络台的实例
        mNetStation = ((RadioButton) view.findViewById(R.id.net_station));
        //获得省市台的实例
        mProvinceStation = ((RadioButton) view.findViewById(R.id.province_city_station));
    }

    /**
     * 填充的是radio的电视台的数据
     */
    private void initData() {
        list = new ArrayList<>();
        list1 = new ArrayList<>();

    }

    /**
     * 下载数据
     */
    private void downLoadData() {

        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getText().enqueue(this);


    }


    @Override
    public void onResponse(Call<RadioEntity> call, Response<RadioEntity> response) {
        RadioEntity radioEntity = response.body();
        /**
         * 将第一个listview里面的数据分离开来
         */
        List<RadioEntity.RadiolocalRadiosEntity> localRadios = new ArrayList<>();
        List<RadioEntity.RadiotopRadiosEntity> topRadios = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            localRadios.add(radioEntity.getData().getLocalRadios().get(i));
            topRadios.add(radioEntity.getData().getTopRadios().get(i));

        }
        mradioFristList.setAdapter(new RadioFristListAdapter(getContext(), localRadios));
        mradioSecondList.setAdapter(new RadioSeconListAdapter(getContext(), topRadios));
        for (int i = 0; i < 7; i++) {
            list1.add(radioEntity.getData().getCategories().get(i).getName());
        }

        for (int i = 0; i < 15; i++) {
            list.add(radioEntity.getData().getCategories().get(i).getName());
        }

        mRecycleview.setLayoutManager(new GridLayoutManager(getContext(), 4));
        adapter = new RadioAdapter(list, getContext(), list1);
        mRecycleview.setAdapter(adapter);
        adapter.setOnItemClickListener(new RadioAdapter.MyItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                mImage = (ImageView) view.findViewById(R.id.id_image);
                if (position == list1.size() && list1.size() == 7) {
                    for (int i = 7; i < list.size(); i++) {
                        list1.add(list.get(i));
                    }

                    mImage.setImageResource(R.mipmap.ic_triangle_up);
                } else if (position == list1.size() && list1.size() == 15) {
                    list1.removeAll(list1);
                    for (int i = 0; i < 7; i++) {
                        list1.add(list.get(i));
                    }
                    mImage.setImageResource(R.mipmap.ic_triangle_down);
                }
                adapter.notifyDataSetChanged();

            }
        });

    }

    @Override
    public void onFailure(Call<RadioEntity> call, Throwable t) {

        //Log.i("1620", "数据加载错误");
    }

    /**
     * 添加一个监听器来跳转到相应的界面
     * @param v
     */
    @Override
    public void onClick(View v) {
        Bundle bundle=new Bundle();
        switch (v.getId()) {
            case R.id.home_station:
                bundle.putString("station","本地");
                listener.openDraw("11",bundle);
                break;
            case R.id.country_station:
                bundle.putString("station","国家");
                listener.openDraw("11",bundle);
                break;
            case R.id.net_station:
                bundle.putString("station","网络");
                listener.openDraw("11",bundle);
                break;
            case R.id.province_city_station:
                bundle.putString("station","省市台");
                listener.openDraw("12",bundle);
                break;
    }



    }

}
